package com.example.demo.timer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * Created on 2019/9/13.
 *
 * @author Grak
 * @since 1.0
 */
@Component
public class TimerTask {

    @Autowired
    DealHave dealHave;

    public void getFutureMessage() throws ExecutionException, InterruptedException {
        Future<String> future = dealHave.dealHve();
        //任务是否已经执行完毕
        boolean isDone = future.isDone();
        //任务是否取消
        boolean isCanceller = future.isCancelled();
        //取消任务(true 强制停止，false已经开始的任务不会停止)
//            future.cancel(true);
        //获取结果的时候会柱塞等待结果
        String chuju = future.get();
    }

    private long time = new Date().getTime();

    @Scheduled(fixedRate = 4000) //上次任务开始后4秒执行
//    @Scheduled(fixedDelay = 4000) //上次任务结束后4秒执行
//    @Scheduled(initialDelay = 1000,fixedDelay = 4000) //第一次延迟1秒后执行，之后每隔4秒执行
//    @Scheduled(cron = "*/5 * * * * *") //corn表达式定义
    public void showCurrentTime(){
        System.out.println("执行时间：" +  (new Date().getTime() - time));
    }
}
